import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
//Creates the full InventoryItems
public class VisualInventory implements ActionListener {

	private JFrame scanFrame = null;  //  @jve:decl-index=0:visual-constraint="211,86"

	private JPanel scanContentPane = null;

	private JScrollPane notesScrollPane = null;

	private JButton submitButton = null;

	private JTextField serialTextField = null;
	private JTextArea notesTextArea = null;

	private JLabel hintLabel = null;
	private JLabel typeLabel = null;
	private JLabel modelLabel = null;
	private JLabel notesLabel = null;

	private JComboBox deviceComboBox = null;
	private JComboBox modelComboBox = null;

	private String location, user;
	private String[] deviceList = {"SELECT DEVICE", "Laptop", "Desktop","Monitor", "Printer" , "Q - Terminal", "Q - VTouch", "Q - Printer",  "Keyboard", "Other" };
	private String[] desktopList = {"SELECT MODEL", "HP 3130", "A62", "A61e", "A60", "S51", "S50", "HP d530", "Other" };
	private String[] laptopList = {"SELECT MODEL", "Dell E6410", "HP" , "T400", "T61", "Z61t", "Z60t", "X41", "X60", "Acer Netbook" , "HP Netbook", "Other" } ;
	private String[] monitorList = {"SELECT MODEL", "22 inch", "19 inch", "17 inch", "15 inch", "Other" };

	private int indexInt = 0;

	private boolean firstStart = true;

	private InventoryList iL;  
	private boolean rebuildListGUI;
	//CONSTRUCTOR
	public VisualInventory(String loc, String it){
		location = loc;
		user = it;
		this.buildGUI();
//		System.out.println("Location: " + location + " User: " + user  );

	}
	//Constructs GUI	
	private JFrame getScanFrame() {
		if (scanFrame == null) {
			scanFrame = new JFrame();
			scanFrame.setSize(new Dimension(556, 326));
			scanFrame.setTitle("Indians I.S. Inventory -- SDonaldsonJr ");
			scanFrame.setContentPane(getScanContentPane());
		}
		return scanFrame;
	}	

	private JPanel getScanContentPane() {
		if (scanContentPane == null) {

			notesLabel = new JLabel();
			notesLabel.setBounds(new Rectangle(31, 193, 54, 16));
			notesLabel.setText("Notes: ");

			modelLabel = new JLabel();
			modelLabel.setBounds(new Rectangle(64, 150, 57, 16));
			modelLabel.setText("Model: ");

			typeLabel = new JLabel();
			typeLabel.setBounds(new Rectangle(63, 108, 49, 16));
			typeLabel.setText("Type: ");

			hintLabel = new JLabel();
			hintLabel.setBounds(new Rectangle(62, 32, 223, 30));
			hintLabel.setText("Scan  or Enter Inventory Item: ");

			scanContentPane = new JPanel();
			scanContentPane.setLayout(null);
			scanContentPane.add(getSubmitButton(), null);
			scanContentPane.add(getSerialTextField(), null);
			scanContentPane.add(hintLabel, null);
			scanContentPane.add(getDeviceComboBox(), null);
			scanContentPane.add(typeLabel, null);
			scanContentPane.add(modelLabel, null);
			scanContentPane.add(getModelComboBox(), null);
			scanContentPane.add(notesLabel, null);
			scanContentPane.add(getNotesScrollPane(), null);
		}
		return scanContentPane;
	}

	private JButton getSubmitButton() {
		if (submitButton == null) {
			submitButton = new JButton();
			submitButton.setBounds(new Rectangle(417, 63, 108, 32));
			submitButton.setText("SUBMIT");
		}
		return submitButton;
	}

	private JTextField getSerialTextField() {
		if (serialTextField == null) {
			serialTextField = new JTextField();
			serialTextField.setBounds(new Rectangle(62, 63, 348, 33));
		}
		return serialTextField;
	}

	private JComboBox getDeviceComboBox() {

		if (deviceComboBox == null) {

			deviceComboBox = new JComboBox(deviceList);
			deviceComboBox.setBounds(new Rectangle(119, 105, 132, 25));
			deviceComboBox.setMaximumRowCount(3);

		}
		return deviceComboBox;
	}

	private JComboBox getModelComboBox() {
		if (modelComboBox == null) {
			modelComboBox = new JComboBox();
			modelComboBox.setBounds(new Rectangle(119, 146, 132, 25));
			this.enableModel(false);
		}
		return modelComboBox;
	}

	private JTextArea getNotesTextArea() {
		if (notesTextArea == null) {
			notesTextArea = new JTextArea();
			notesTextArea.setWrapStyleWord(true);

		}
		return notesTextArea;
	}

	private JScrollPane getNotesScrollPane() {
		if (notesScrollPane == null) {
			notesScrollPane = new JScrollPane();
			notesScrollPane.setBounds(new Rectangle(92, 192, 405, 83));
			notesScrollPane.setViewportView(getNotesTextArea());
		}
		return notesScrollPane;
	}

	private void buildGUI(){

		this.getScanFrame();

		scanFrame.setVisible(true);
		submitButton.addActionListener(this);
		serialTextField.addActionListener(this);
		deviceComboBox.addActionListener(this);
		modelComboBox.addActionListener(this);
	
		this.enableNotes(false);
		this.enableModel(false);
		this.enableSerial(false);
	}

	private void buildInventoryItem(String sn, String item, String modelType, String notes){
		InventoryItem iI = new InventoryItem(sn,item,modelType,notes,this.location,this.user);
		iL.addToList(iI);

	}

	private void enableNotes(boolean b){
		this.notesLabel.setVisible(b);
		this.notesTextArea.setVisible(b);
		this.notesScrollPane.setVisible(b);
		this.notesTextArea.setText("");
	}

	private void enableModel(boolean b){
		this.modelComboBox.setVisible(b);
		this.modelLabel.setVisible(b);
	}

	private void enableSerial(boolean b){
		this.serialTextField.setEditable(b);
		this.submitButton.setVisible(b);
	}
	

	//ON CLICK ACTIONS
	@Override
	public void actionPerformed(ActionEvent arg0){
		// TODO Auto-generated method stub
		String sn, device, model, notes;

		//Sends the Serial and Selected Information to the Database
		if(arg0.getSource() == submitButton || arg0.getSource() == serialTextField){
			indexInt++;

			//Builds Inventory List UI
			if(firstStart == true){
				iL = new InventoryList(this,location, user);
				firstStart = false;

//				System.out.println("Creating inventoryList");
			}
			
			if(rebuildListGUI == true){
				
			}
			

			if(this.serialTextField.getText() != null){
				sn = this.serialTextField.getText();

			}
			else{
				sn = "---";
			}

			if(this.deviceComboBox.getSelectedItem() != null ){
				device = this.deviceComboBox.getSelectedItem().toString();

			}
			else{
				device = "---";
			}
			if(this.modelComboBox.getSelectedItem() != null){
				model = this.modelComboBox.getSelectedItem().toString();
			}
			else{
				model = "---";
			}

			if(this.notesTextArea.getText() != null){
				notes = notesTextArea.getText();
			}
			else{
				notes = "---";
			}
//			System.out.println(sn+" "+device+" "+model+" "+notes);

			buildInventoryItem(sn, device, model, notes);
			this.serialTextField.setText("");

		} // Ends Submit Conditionals

		//Has the deviceComboBox enable the modelComboBox and populate it with relative data. 
		if(arg0.getSource() == deviceComboBox){

			if(this.deviceComboBox.getSelectedItem() == "SELECT DEVICE"){
				this.modelComboBox.removeAllItems();

				this.enableSerial(false);
				this.enableModel(false);
				this.enableNotes(false);
			}

			else if(this.deviceComboBox.getSelectedItem() == "Laptop"){
				modelComboBox.removeAllItems();
				for (int i = 0 ; i < laptopList.length; i++){
					modelComboBox.addItem(laptopList[i]);
				}
				this.enableModel(true);
				this.enableSerial(false);
			}

			else if(this.deviceComboBox.getSelectedItem() == "Desktop"){
				modelComboBox.removeAllItems();
				for (int i = 0 ; i < desktopList.length; i++){
					modelComboBox.addItem(desktopList[i]);
				}
				this.enableModel(true);
				this.enableSerial(false);
			}
			else if(this.deviceComboBox.getSelectedItem() == "Monitor"){
				modelComboBox.removeAllItems();
				for(int i = 0; i < monitorList.length; i++){
					modelComboBox.addItem(monitorList[i]);
				}
				this.enableModel(true);
				this.enableSerial(false);

			}
			else if(this.deviceComboBox.getSelectedItem()== "Other"){
				this.enableModel(false);
				this.enableNotes(true);
				this.enableSerial(true);
			}
			else{
				this.modelComboBox.removeAllItems();
				this.enableModel(false);
				this.enableNotes(false);
				this.enableSerial(true);
			}
		}// Ends deviceComboBox Conditionals

		if(arg0.getSource() == modelComboBox){
			if(this.modelComboBox.getSelectedItem() == "Other"){
				this.enableNotes(true);
				this.enableSerial(true);
			}
			else if(modelComboBox.getSelectedItem() == "SELECT MODEL"){
				this.enableSerial(false);

			}
			else{
				this.enableNotes(false);
				this.enableSerial(true);

			}
		} //Ends modelComboBox Conditionals
	}//Ends ActionPerformed

} // Ends Class